package com.Blog.www.dao.Impl;

import com.Blog.www.constant.SqlConstant;
import com.Blog.www.dao.UserDao;
import com.Blog.www.po.User;
import com.Blog.www.utils.SqlDao;
import org.springframework.dao.DataAccessException;

import java.util.Date;
import java.util.List;

/**
 * @author 君未洋
 */
public class UserDaoImpl implements UserDao {
    private static SqlDao sqlDao = new SqlDao();

    @Override
    public User login(User loginUser) {
        String sql = "SELECT id,account,username,AES_DECRYPT(PASSWORD,'k') AS PASSWORD,STATUS,identity,path,lasttime" +
                " FROM USER where account=? and AES_DECRYPT(PASSWORD,'k')=?";
        return sqlDao.selectUser(sql, loginUser.getAccount(), loginUser.getPassword());
        //如果有这个账号就返回这个账号的信息

    }

    @Override
    public Boolean regist(User registuser, String identity) {
        String sql = "INSERT INTO USER VALUES (null,?,?,AES_ENCRYPT(?,'k'),?,?,'null.jpg',?)";
        //写入sql
        return sqlDao.update(sql, registuser.getAccount(), registuser.getUsername(), registuser.getPassword(), SqlConstant.STATUS_B, identity,System.currentTimeMillis());
        //成功返回true
    }


    @Override
    public Boolean getAccount(int account) {
        try {
            String sql = "select account from user where account = ?";
            Integer uid = sqlDao.selectAccount(sql, account);
            if (uid == 0 || "".equals(uid) || uid == null) {
                return false;
            } else {
                return true;
            }
            //如果有这个账号就返回true表示有重复
        } catch (DataAccessException e) {
            return false;
            //这里是因为数据会报异常,所以捕获一下
        }
    }

    @Override
    public int findUserTotalCount(String status, String identity) {
        String sql = "select count(*) as count from user where status = ? and identity = ?";
        return sqlDao.selectCount(sql, status, identity);
        //使用这个方法可以直接返回int类型的数据
    }

    @Override
    public List<User> findUserList(int start, int rows, String status, String identity) {
        //查询
        String sql = "select * from user where status =? and identity = ? limit ? , ? ";
        return sqlDao.selectAllUser(sql, status, identity, start, rows);
    }

    @Override
    public Boolean changerUser(int id, String status) {
        String sql = "update user set status = ?  where id=?";
        return sqlDao.update(sql, status, id);
    }

    @Override
    public User getUser(String username, String account) {
        String sql = "SELECT id,account,username,AES_DECRYPT(PASSWORD,'k') AS PASSWORD,STATUS,identity,path,lasttime" +
                " FROM USER where username=? and account=?";
        return sqlDao.selectUser(sql, username, account);
        //如果有这个账号就返回这个账号的信息
    }

    @Override
    public User getUser(int id, String status) {
        if (status == null) {
            String sql = "SELECT id,account,username,AES_DECRYPT(PASSWORD,'k') AS PASSWORD,STATUS,identity,path,lasttime" +
                    " FROM USER where id=? ";
            return sqlDao.selectUser(sql, id);
        } else {
            String sql = "SELECT id,account,username,AES_DECRYPT(PASSWORD,'k') AS PASSWORD,STATUS,identity,path,lasttime" +
                    " FROM USER where id=? and status=?";

            return sqlDao.selectUser(sql, id, status);
        }
        //如果有这个账号就返回这个账号的信息
    }

    @Override
    public boolean changePassword(int id, String password) {
        String sql = "UPDATE USER SET PASSWORD = AES_ENCRYPT(?,'k')  WHERE id=?";
        return sqlDao.update(sql, password, id);
    }

    @Override
    public Boolean delete(String id) {
        String sql = "delete from user where id = ?";
        return sqlDao.update(sql, id);
    }

    @Override
    public Boolean addImg(String imgPath, User user) {
        String sql = "update user set path = ? where id = ?";
        return sqlDao.update(sql, imgPath, user.getId());
    }

    @Override
    public boolean banTime(String userId, Long time) {
        String sql = "update user set lasttime = ? where id = ?";
        return sqlDao.update(sql,time,userId);
    }


}
